package com.controller.server;

import com.util.Printer;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

/**
 * Created by Phillip on 2014/4/22.
 */
public class CORBAServer {
    public static void main(String[] args) {
        ORB orb = ORB.init(args, null);
        CORBAServant servant = new CORBAServant();
        orb.connect(servant);
        try {
            org.omg.CORBA.Object objectRef = orb.resolve_initial_references("NameService");
            NamingContext namingContext = NamingContextHelper.narrow(objectRef);
            NameComponent nameComponent = new NameComponent("corba.airlineServer.Server", "");
            NameComponent[] path = {nameComponent};
            namingContext.rebind(path, servant);
            Printer.p("CORBA server is up...");
            Object syncObj = new Object();
            synchronized (syncObj) {
                syncObj.wait();
            }
        } catch (InvalidName invalidName) {
            invalidName.printStackTrace();
        } catch (org.omg.CosNaming.NamingContextPackage.InvalidName invalidName) {
            invalidName.printStackTrace();
        } catch (CannotProceed cannotProceed) {
            cannotProceed.printStackTrace();
        } catch (NotFound notFound) {
            notFound.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
